﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace assignment5
{
    
    internal class OrderService
    {
        static List<Order>orders = new List<Order>();

        //添加订单明细
        public void AddOrder(Order order)
        {
            if (!orders.Contains(order))
            {
                orders.Add(order);
                
                Console.WriteLine("添加完成！");
            }
            else
            {
                Console.WriteLine("已添加过此订单！");
            }
        }

        //删除订单
        public void DeleteOrder(int id)
        {
                var query = from o in orders
                            where o.OrderID == id
                            select o;
                Order order2 = query.FirstOrDefault();
               
            if (order2 != null)
            {
                orders.Remove(order2);
                
                Console.WriteLine("删除成功");
            }
            else
            {
                Console.WriteLine("删除失败");
            }

        }

        //修改订单
        public void ChangeOrder(Order order1)
        {
            
                int num = order1.OrderID;
                var query = from o in orders
                            where o.OrderID == num
                            select o;
                Order order3 = query.FirstOrDefault();
            if(order3 != null)
            {
                order3.OrderID = order1.OrderID;
                order3.Customer = order1.Customer;
                order3.TotalMoney = order1.TotalMoney;
                Console.WriteLine("订单修改成功");
            }
            else
            {
                Console.WriteLine("订单修改失败");
            }
            
        }

        //查询订单
        public void SearchOrder(int id)
        {

            switch (id)
            {
                case 1:
                    Console.WriteLine("请输入订单号");
                    int num = Int32.Parse(Console.ReadLine());
                    var query = from o in orders
                                where o.OrderID == num
                                select o;
                    Order order4 = query.FirstOrDefault();
                    if (order4 != null)
                    {
                        Console.WriteLine("订单信息：");
                        order4.ToString(order4);
                    }
                    else
                        Console.WriteLine("查询失败");
                    break;

                case 2:
                    Console.WriteLine("请输入商品名称");
                    string name = Console.ReadLine();
                    Order order5;
                    foreach (var o in orders)
                    {
                        foreach(var od in o.details)
                        {
                            if(od.ItemName == name)
                            {
                                order5 = o;
                                if (order5 != null)
                                {
                                    Console.WriteLine("订单信息：");
                                    order5.ToString(order5);
                                }
                                else
                                    Console.WriteLine("查询失败");
                            }
                            
                        }
                    }
                    
                    break;
                case 3:
                    Console.WriteLine("请输入客户名");
                    string cName = Console.ReadLine();
                    var query2 = from o in orders
                                 where o.Customer == cName
                                 select o;
                    Order order6 = query2.FirstOrDefault();
                    if (order6 != null)
                    {
                        Console.WriteLine("订单信息：");
                        order6.ToString(order6);
                    }
                    else
                        Console.WriteLine("查询失败");
                    break;
                case 4:
                    Console.WriteLine("请输入商品总金额");
                    int amount = Int32.Parse(Console.ReadLine());
                    var query3 = from o in orders
                                 where o.TotalMoney == amount
                                 select o;
                    Order order7 = query3.FirstOrDefault();
                    if (order7 != null)
                    {
                        Console.WriteLine("订单信息：");
                        order7.ToString(order7);
                    }
                    else
                        Console.WriteLine("查询失败");
                    break;
                default:
                    Console.WriteLine("输入错误！");
                    break;
            }
        }
        //查询订单详细信息
        public void SearchOrderDetails(int id)
        {
            var q = from d in orders
                    where d.OrderID == id
                    select d;
            Order order7 = q.FirstOrDefault();
            if (order7 != null)
            {
                order7.ToString(order7);
                foreach(OrderDetails od in order7.details)
                {
                    od.ToString(od);
                }
            }
            else
                Console.WriteLine("查询失败");
        }

        //按订单号排序
        public void SetInOrder()
        {
            var query = from o in orders
                        orderby o.OrderID
                        select o;
            orders = query.ToList();
        }
    }
}
